﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Demo</title>
	<!-- 假设已正确引入easyui相关的css文件，示例中暂未给出完整引入代码 -->
	<link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body class="layui-padding-3">
	<div>
		<form class="layui-form" action="">
			<div class="layui-form-item">
				<label class="layui-form-label">单据编号:</label>
				<div class="layui-input-block">
					<input type="text" Id="documentNumber" lay-verify="required" placeholder="请输入，忽略将自动生成" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">备注:</label>
				<div class="layui-input-block">
					<input type="text" Id="remarks" lay-verify="required" placeholder="备注" autocomplete="off" class="layui-input">
				</div>
			</div>
		</form>
	</div>
	生产计划-产品明细
	<table class="layui-hide" id="ID-table-demo-templet"></table>

	<button type="button" class="layui-btn" lay-filter="Add">保存</button>
	<button type="reset" class="layui-btn layui-btn-primary">关闭</button>
	<script type="text/html" id="toolbarDemo">
		<button class="layui-btn layui-btn-sm" lay-on="test-alert-btn1" >+选择产品</button>
		<button class="layui-btn layui-btn-sm" >删除行</button>
		<button class="layui-btn layui-btn-sm" >导入</button>
		<button class="layui-btn layui-btn-sm" >刷新</button> 
	</script>
	<!-- 假设已正确引入easyui相关的js文件，示例中暂未给出完整引入代码 -->
	<script src="//unpkg.com/layui@2.9.20/dist/layui.js"></script>
</body>
</html>
<script>

	layui.use(['table', 'layer'], function () {
		var table = layui.table;
		var layer = layui.layer;
		var mockData = JSON.parse(localStorage.getItem("ProductData"));//数据源
		var $ = layui.$;

		// 渲染
		table.render({
			elem: '#ID-table-demo-templet',
			data: mockData,
			page: true,
			cols: [[
				{ type: 'checkbox', fixed: 'left' },
				{
					field: 'productNumber', width: 180, title: '产品编号', templet: function (d) {
						// 直接设置字体颜色为蓝色
						return '<a href="" style="color:blue; target="_blank">' + d.productNumber + '</a>';
					}, sort: true
				},
				{ field: 'productName', width: 180, title: '产品名称', },
				{ field: 'productSpecification', width: 180, title: '产品规格', },
				{ field: 'orderNo', width: 180, title: '工单编号', },
				{
					field: 'quantity', width: 180, title: '数量', templet: function (d) {
						// 直接设置字体颜色为蓝色
						return '<input type="text" Id="quantity" lay-verify="required" value="' + 1 + '"   autocomplete="off" class="layui-input">';
					}
				},
				{ field: 'completedQuantity', width: 180, title: '完成数', },
				{ field: 'createDate', width: 180, title: '创建时间', },
				{ field: 'createName', width: 180, title: '创建人', },
				{ field: 'updateName', width: 180, title: '修改人', },
				{ field: 'updateDate', width: 180, title: '修改时间', }
			]],
			toolbar: '#toolbarDemo',
			initSort: { // 设置初始排序
				field: 'experience', // 字段名
				type: 'desc' // 倒序
			},
			height: 366
		});


		function Add() {
			console.log("执行保存操作");
			var planProductDetails = [];
			var checkedCheckboxes = $("table.layui-table tbody input[type='checkbox'][name='layTableCheckbox']:checked");
			var count = checkedCheckboxes.length;
			count = count / 2;
			$("table.layui-table tbody input[type='checkbox'][name='layTableCheckbox']:checked").each(function () {
				var row = $(this).closest('tr');
				var rowData = {
					'productNumber': row.find('td').eq(1).text(),
					'documentNumber': "",
					'orderNo': "",
					'quantity': row.find('input[type="text"][id="quantity"]').val(),
					'completedQuantity': 0,
					'createId': 1,
					'createName': "小罗",
					'createDate': new Date().toISOString(),
					'updateId': null,
					'updateName': "",
					'updateDate': null,
					'deleteId': null,
					'deleteName': "",
					'deleteDate': false
				};
				planProductDetails.push(rowData);
			});
			var firstTwoDetails = planProductDetails.slice(0, count)
			console.log("选中的行数据：", firstTwoDetails);
			 

			var productionPlans ={ 
				"documentNumber": "",
				"remarks": $("#remarks").val(),
				'createId': 1, //
				'createName': "小罗", //
				'createDate': new Date().toISOString(), //
				'updateId': null, //
				'updateName': "", //
				'updateDate': null, //
				'deleteId': null, //
				'deleteName': "", //
				'deleteDate': false, //
			}

			var data = {
				productionPlans: productionPlans,
				planProductDetails: firstTwoDetails

			}
			console.log("选中的行数据：", JSON.stringify(data));
			$.ajax({
				url: 'http://localhost:5167/api/Department/ProductionPlanCreate',
				type: 'POST',
				data: JSON.stringify(data),
				contentType: 'application/json',
				success: function (result) {
					if (result.code == 0) {
						layer.msg(result.messege);
						// 关闭当前layer弹窗
						parent.layer.closeAll(); // 关闭所有弹窗，如果只需要关闭当前弹窗，需要传递index
						// 刷新父页面
						parent.location.reload();
					} else {
						layer.msg(result.messege);
					}
				},
				error: function (xhr, status, error) {
					console.log(xhr.responseText);
				}
			});  
			
		}


		//弹框
		layui.use(function () {
			var layer = layui.layer;
			var util = layui.util;
			// 事件
			util.on('lay-on', {
				'test-alert-btn1': function () {
					layer.open({
						type: 2,
						area: ['1300px', '600px'],
						shadeClose: true,
						title: '选择产品',
						content: '/Production/ProductionShow',
					});
				}
			});
		});
		// 监听表格之外的按钮点击事件
		$('.layui-btn[lay-filter="Add"]').click(function () {
			// 判断是否选中数组
			if ($('input[name="layTableCheckbox"]:checked').length === 0) {
				layer.msg('请选择要操作的数据');
				return;
			}
			// 执行保存操作
			Add();; // 假设这是您定义的函数，用于处理选中的数据
		});
		 

	});

	

</script>